package utils;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import jsonobj.JobInfo;

import data.WebSocket;

public class Utils {

	public static WebSocket ws;
	public static String username;
	public static String passwd;
	public static Map<String,String> job_names = new HashMap<String,String>();
	public static ExecutorService exec = Executors.newCachedThreadPool();
	public static Runtime run = Runtime.getRuntime();
	public static void out(Object o) {
		System.out.println(o.toString());
	}

	public static void createPBSfile(JobInfo ji) {
		try {
			new java.io.File(ji.jname + "/PBS"+ ji.jname).delete();
			PrintWriter pw = new PrintWriter(new FileWriter(ji.jname + "/PBS"
					+ ji.jname),true);
			pw.println("#!/bin/sh");
			pw.println("#PBS -N " + ji.jname);
			pw.println("#PBS -j oe");
			pw.println("#PBS -o ./" + ji.jname + "/" + ji.jname + ".out");
			pw.println("#PBS -l nodes=" + ji.nodes + ":ppn=" + ji.procs
					+ ",walltime=" + ji.walltime);
			pw.println("cd $PBS_O_WORKDIR");
			pw.println("cd "+ ji.jname);
			pw.println(ji.exec);
			if (ji.nodes > 1) {
				pw.println("while read line");
				pw.println("do");
				pw.println("echo ${line%%.*} >> nodefile."+ ji.jname);
				pw.println("done < $PBS_NODEFILE");
				pw.println("mpdboot -n " + ji.nodes + " -f nodefile."
						+ ji.jname);
				pw.println("mpirun -machinefile nodefile." + ji.jname
						+ " -np " + ji.nodes * ji.procs + " "+ ji.exec);
				pw.println("mpdallexit");
			}
			/*} else if (ji.isMPI) {
			 * pw.println("mpirun -machinefile nodefile." + ji.jname
						+ " -n " + ji.nodes * ji.procs + " sire -n "
						+ ji.nmoves + " -r " + ji.repeat + " " + ji.filename);
				pw.println("mpdboot");
				// pw.println("cat $PBS_NODEFILE > machine.file.$PBS_JOBID");
				pw.println("mpirun -np " + ji.nodes * ji.procs + " " + ji.cmd);*/
			
			pw.close();
		} catch (Exception e) {
		}
	}
	
	public static String runCmd(String cmd) throws Exception
	{
		String[] cmdarr = {
				"/bin/sh",
				"-c",
				cmd };
		Process p = run.exec(cmdarr);
		BufferedReader input = new BufferedReader(
				new InputStreamReader(p.getInputStream()));
		String s = input.readLine();
		input.close();
		p.waitFor();
		p.destroy();
		return s;
	}

	public static String[] runOneCmd() throws IOException,
			InterruptedException {
		String[] data = new String[4];
		Process child = Runtime.getRuntime().exec("showstats -v");
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				child.getInputStream()));
		// String s = "";
		String line;
		while ((line = reader.readLine()) != null) {
			// System.out.println("Stdout: " + line);
			if (line.contains("Active Jobs:")) {
				// jobs
				data[0] = line.substring("Active Jobs:".length() + 1).trim();
			}else if (line.contains("Eligible/Idle Jobs")){
					String g = line
					.substring("Eligible/Idle Jobs:".length() + 1)
					.trim();
				data[1] = g.split("/")[0]; 
			} else if (line.contains("Current Active/Total Procs:")) {
				data[2] = line.substring(
						"Current Active/Total Procs:".length() + 1,
						line.indexOf('(')).trim();
			} else if (line.contains("Current Active/Total Nodes:")) {
				data[3] = line.substring(
						"Current Active/Total Nodes:".length() + 1,
						line.indexOf('(')).trim();
			}
		}
		child.waitFor();
		return data;
	}
	
	public static int ConvertWalltime(String w)
	{
		String [] a = w.split(":");
		return Integer.parseInt(a[2])+ Integer.parseInt(a[1])*60 + Integer.parseInt(a[0])*3600;
	}
}
